 
 	<?php
		function set_val($obj, $val, $return='echo')
		{
			if(isset($obj))
				if(isset($obj->$val))
					if($return=='echo')
						echo($obj->$val);
					else
						return $obj->$val;
		}
	?>
 
				<div><br>
					<form id="form" class="block-content form" action="<?php echo site_url('admin/role/save_role_permission')?>" method="post">
						<input type="hidden" id="id" name="id" value="<?php if(isset($id)) echo $id;?>" />
						
						<?php 
						function set_role($role,$array)
						{
							if(isset($role[$array['fun']])){
								if(@$role[$array['fun']] == $array['id'])
									return true;
							}else
								return false;
						}
						
						?>
						<?php foreach($permissions as $controller=>$array):?>

							<div class="_50">
								<fieldset>
								<legend><?php echo $controller?></legend>	
								<?php foreach($array as $val):?>
									
									<p><label>
										<input <?php if(set_role(@$role[$controller],$val)) echo 'checked="checked"'; ?> type="checkbox" name="checkbox[]" value="<?php echo $val['id']?>"/> 
										<?php echo $val['name']?>
									</label></p>
								<?php endforeach;?>
								</fieldset></div>
						<?php endforeach;?>
						

						<div class="clear"></div>
						<div class="block-actions">
							<ul class="actions-left">
								<li><a class="button red" id="reset-validate-form" href="javascript:void(0);">Reset</a></li>
							</ul>
							<ul class="actions-right">
								<li><input type="submit" class="button" value="Save!"></li>
							</ul>
						</div>
					</form>

				</div>
				
				
	<script type="text/javascript">
						
		
	$().ready(function() {
		
		/** Submit Form **/
		$("#form").validate({
				rules:{},
				messages: {},
				submitHandler: function(form) {
				   //submit
				$.post("<?php echo site_url('admin/role/save_role_permission')?>", $("#form").serialize(),
					function(data){
						if(data){
							
							newdata = $.parseJSON(data);
							
							if(newdata.error){
								Gtheme = 'error';
								
								$.each(newdata.msg, function(index) {
									$.jGrowl(newdata.msg[index], { theme: Gtheme,position:'center'  });
								});

							}
							else{
								Gtheme = 'saved';
								$.jGrowl(newdata.msg, { theme: Gtheme,position:'center'  });
								
								temp_id 	= $("#id").val();
								temp_name 	= TITLE;
								//clear the folrm
									$("#id").val('');
									$(":input").not(":button, :submit, :reset, :hidden").each(function () {
										this.value = '';
									});
								load_role(temp_id,temp_name);
							}

						}
						else{
							$.jGrowl("Error!", { theme: 'error' });
						}
					});
				}
			});
		/** End Submit Form **/
		
		});
		
		
    </script>

